package com.jxhobo;

import java.util.List;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.SaveCallback;
import com.jxhobo.bean.User;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;

public class AppVersionManager {
	String version;
	Context context;
	AlertDialog.Builder upgradeDialog;
	 
	public AppVersionManager(String version, final Context context) {
		super();
		this.version = version;
		this.context = context;
		upgradeDialog = new AlertDialog.Builder(context);
		upgradeDialog.setTitle("有新版本!")
    	.setIcon(android.R.drawable.ic_dialog_info)
    	.setMessage("是否现在下载更新?")
    	.setPositiveButton("是", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.jxhobo.com/app_download")));
				((SelectPage)context).finish();
			}
		})
		.setNegativeButton("否", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				}
		});
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	/**  
	* 返回当前程序版本名  
	*/   
	public void getAppVersionName() {   
	    String versionName = "";   
	    try {   
	        // ---get the package info---   
	        PackageManager pm = context.getPackageManager();   
	        PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);   
	        versionName = pi.versionName;   
	        if (versionName == null || versionName.length() <= 0) {   
	            return;
	        }
	        setVersion(versionName);
	        //Toast.makeText(context, "version = " + versionName, Toast.LENGTH_SHORT).show();
	    } catch (Exception e) {   
	        Log.e("VersionInfo", "Exception", e);   
	    }   
	}
	
	public void saveVersionToCloud(final Context context) {
		AVObject version = new AVObject("LatestVersion");
		version.put("version", getVersion());
		version.saveInBackground(new SaveCallback() {
			
			@Override
			public void done(AVException e) {
				// TODO Auto-generated method stub
				if (e != null) {
					Toast.makeText(context, "存取版本失败，请检查网络！！！", Toast.LENGTH_SHORT).show();
				}
			}
		});
	}
	
	public void compareVersion() {
		getAppVersionName();
		
		AVQuery<AVObject> query = new AVQuery<AVObject>("LatestVersion");
		query.findInBackground(new FindCallback<AVObject>() {
		    public void done(List<AVObject> avObjects, AVException e) {
		        if (e == null) {
		            Log.d("成功", "查询到" + avObjects.size() + " 条符合条件的数据");
		            AVObject latestVersion = avObjects.get(0);
		            String latest = latestVersion.getString("version");
		            if (Float.valueOf(latest).floatValue() > Float.valueOf(getVersion()).floatValue()) {
		            	upgradeDialog.show();
		            }
		            
		        } else {
		            Log.d("失败", "查询错误: " + e.getMessage());
		            Toast.makeText(context.getApplicationContext(), "比较版本信息失败，请检查网络！", Toast.LENGTH_SHORT).show();
		        }
		    }
		});
	}
}
